/*
Copyright (c) 2012, Janez Žemva
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
o The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ETL_QUATERNION_HPP
# define ETL_QUATERNION_HPP
# pragma once

#include "matrix.hpp"

namespace etl
{

template <typename, typename>
struct quat_expression;

template <typename T>
struct quat : quat_expression<T, quat<T> >
{
  using value_type = T;
  using size_type = ::std::size_t;

  using iterator = T*;
  using const_iterator = T const*;
  using difference_type = ::std::ptrdiff_t;

  using reverse_iterator = ::std::reverse_iterator<iterator>;
  using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;

  static constexpr size_type const N = 4;
  static constexpr size_type const dimension = N;

  constexpr quat() = default;

  constexpr quat(quat const&) = default;
  constexpr quat(quat&&) = default;

  template <typename U,
    typename = typename ::std::enable_if<
      !::std::is_same<quat, typename ::std::decay<U>::type>{}
    >::type
  >
  quat(U&& u)
  {
    *this = ::std::forward<U>(u);
  }

  template <typename ...U,
    typename = typename ::std::enable_if<detail::all_of<
      ::std::is_same<T, typename ::std::decay<U>::type>...>{} &&
      (N == sizeof...(U))
    >::type
  >
  constexpr quat(U... args) : data_{::std::forward<U>(args)...}
  {
  }

  template <typename E>
  quat(quat_expression<T, E>&& other)
  {
    *this = ::std::forward<E>(other);
  }

  constexpr const_iterator cbegin() const { return data_; }
  constexpr const_iterator cend() const { return data_ + dimension; }

  iterator begin() { return data_; }
  iterator end() { return data_ + dimension; }

  constexpr const_iterator begin() const { return data_; }
  constexpr const_iterator end() const { return data_ + dimension; }

  value_type const* cdata() const { return data_; }

  value_type* data() { return data_; }
  value_type const* data() const { return data_; }

  constexpr size_type size() const { return dimension; }

  quat& operator=(quat const&) = default;
  quat& operator=(quat&&) = default;

  quat& operator=(T const& v)
  {
    assign_constant(detail::make_indices<dimension>(), v);

    return *this;
  }

  quat& operator=(::std::initializer_list<T> list)
  {
    assign_initializer<::std::initializer_list<T> >(::std::begin(list),
      detail::make_indices<dimension>());

    return *this;
  }

  template <class Container>
  typename ::std::enable_if<
    detail::is_container<T, Container>{},
    quat&
  >::type
  operator=(Container const& container)
  {
    assert(size() ==
      ::std::distance(::std::begin(container), ::std::end(container)));
    assign_initializer<Container>(::std::begin(container),
      detail::make_indices<dimension>());

    return *this;
  }

  template <typename E>
  quat& operator=(quat_expression<T, E> const& rhs)
  {
    assign_expression(rhs, detail::make_indices<dimension>());

    return *this;
  }

  template <typename E>
  quat& operator+=(quat_expression<T, E> const& rhs)
  {
    add_expression(rhs, detail::make_indices<dimension>());

    return *this;
  }

  template <typename E>
  quat& operator-=(quat_expression<T, E> const& rhs)
  {
    subtract_expression(rhs, detail::make_indices<dimension>());

    return *this;
  }

  quat& operator*=(value_type const s)
  {
    multiply_expression(s, detail::make_indices<dimension>());

    return *this;
  }

  constexpr T cget(size_type const i) const { return data_[i]; }
  T& get(size_type const i) { return data_[i]; } 

  constexpr T operator()(size_type const i) const { return data_[i]; }
  T& operator()(size_type const i) { return data_[i]; }

  template<typename ...U>
  typename ::std::enable_if<
    detail::all_of<::std::is_same<
      typename ::std::decay<U>::type, T>...
    >{}
  >::type
  assign(U&& ...args)
  {
    static_assert((N == sizeof...(U)),
      "wrong number of arguments in assignment");

    assign_constants(detail::make_indices<N>(), ::std::forward<U>(args)...);
  }

private:
  template<size_type... Is>
  void assign_constant(T const& v, detail::indices<Is...> const)
  {
    [](...){}((data_[Is] = v, 0)...);
  }

  template<size_type... Is, typename ...A>
  void assign_constants(detail::indices<Is...> const, A&& ...args)
  {
    [](...){}((data_[Is] = ::std::forward<A>(args), 0)...);
  }

  template<class C, size_type... Is>
  void assign_initializer(typename C::const_iterator i,
    detail::indices<Is...> const)
  {
    ::std::initializer_list<int>{(data_[Is] = *i++, 0)...};
  }

  template<size_type... Is, class E>
  void assign_expression(quat_expression<T, E> const& e,
    detail::indices<Is...> const)
  {
    [](...){}((data_[Is] = e(Is), 0)...);
  }

  template<size_type... Is, class E>
  void add_expression(quat_expression<T, E> const& e,
    detail::indices<Is...> const)
  {
    [](...){}((data_[Is] += e(Is), 0)...);
  }

  template<size_type... Is, class E>
  void subtract_expression(quat_expression<T, E> const& e,
    detail::indices<Is...> const)
  {
    [](...){}((data_[Is] -= e(Is), 0)...);
  }

  template<size_type... Is>
  void multiply_expression(T const s, detail::indices<Is...> const)
  {
    [](...){}((data_[Is] *= s, 0)...);
  }

private:
  T data_[dimension];
};

template <typename T, typename E>
struct quat_expression 
{
public:
  using value_type = T;
  using size_type = ::std::size_t;

  using reference = value_type&;
  using const_reference = value_type const&;

  constexpr value_type operator()(size_type const i) const
  {
    return static_cast<E const&>(*this)(i);
  }

  operator E&() { return static_cast<E&>(*this); }

  constexpr operator E const&() const { return static_cast<E const&>(*this); }
};

template <typename E>
struct quat_vector_part :
  vector_expression<typename E::value_type, 3, quat_vector_part<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr quat_vector_part(E const& u) : u_(u) { }

  constexpr value_type operator()(size_type const i) const
  {
    return u_(i);
  }

  private:
    E const& u_;
};

template <typename E>
struct quat_conjugation :
  quat_expression<typename E::value_type,
  quat_conjugation<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr quat_conjugation(E const& u) : u_(u) { }

  constexpr value_type operator()(size_type const i) const
  {
    return 3 == i ? u_(3) : -u_(i);
  }

  private:
    E const& u_;
};

template <typename E>
struct quat_negation : quat_expression<typename E::value_type,
  quat_negation<E> >
{
  using value_type = typename E::value_type;
  using size_type = typename E::size_type;

  constexpr quat_negation(E const& u) : u_(u) { }

  constexpr value_type operator()(size_type const i) const
  {
    return -u_(i);
  }

  private:
    E const& u_;
};

template <typename E1, typename E2>
struct quat_addition : quat_expression<
  sum_type<typename E1::value_type, typename E2::value_type>,
  quat_addition<E1, E2> >
{
  using value_type = sum_type<typename E1::value_type,
    typename E2::value_type>;
  using size_type = typename E1::size_type;

  constexpr quat_addition(E1 const& u, E2 const& v) : u_(u), v_(v) { }

  constexpr value_type operator()(size_type const i) const
  {
    return u_(i) + v_(i);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename E1, typename E2>
struct quat_subtraction : quat_expression<
  diff_type<typename E1::value_type, typename E2::value_type>,
  quat_subtraction<E1, E2> >
{
  using value_type = diff_type<typename E1::value_type,
    typename E2::value_type>;
  using size_type = typename E1::size_type;

  constexpr quat_subtraction(E1 const& u, E2 const& v) : u_(u), v_(v) { }

  constexpr value_type operator()(size_type const i) const
  {
    return u_(i) - v_(i);
  }

  private:
    E1 const& u_;
    E2 const& v_;
};

template <typename S, typename E>
struct quat_scalar_multiplication : quat_expression<
  prod_type<S, typename E::value_type>,
  quat_scalar_multiplication<S, E> >
{
  using value_type = prod_type<S, typename E::value_type>;
  using size_type = typename E::size_type;

  static_assert(::std::is_same<value_type, S>{}, "");

  constexpr quat_scalar_multiplication(S s, E const& u) : u_(u), s_(s) { }

  constexpr value_type operator()(size_type const i) const
  {
    return s_ * u_(i);
  }

  private:
    E const& u_;
    S s_;
};

template <typename E>
constexpr inline typename E::value_type
norm2(quat_expression<typename E::value_type, E> const& rhs)
{
  return rhs(0) * rhs(0) + rhs(1) * rhs(1) + rhs(2) * rhs(2) +
    rhs(3) * rhs(3);
}

template <typename E>
constexpr inline typename E::value_type
scalar_part(quat_expression<typename E::value_type, E> const& rhs)
{
  return rhs(3);
}

template <typename E>
constexpr inline quat_vector_part<E>
vector_part(quat_expression<typename E::value_type, E> const& rhs)
{
  return quat_vector_part<E>(rhs);
}

template <typename E>
constexpr inline quat_conjugation<E>
conj(quat_expression<typename E::value_type, E> const& rhs)
{
  return quat_conjugation<E>(rhs);
}

template <class E>
constexpr inline quat_negation<E>
operator-(quat_expression<typename E::value_type, E> const& rhs)
{
  return quat_negation<E>(rhs);
}

template <class E1, class E2>
constexpr inline quat_addition<E1, E2>
operator+(quat_expression<typename E1::value_type, E1> const& lhs,
  quat_expression<typename E2::value_type, E2> const& rhs)
{
  return quat_addition<E1, E2>(lhs, rhs);
}

template <class E1, class E2>
constexpr inline quat_subtraction<E1, E2>
operator-(quat_expression<typename E1::value_type, E1> const& lhs,
  quat_expression<typename E2::value_type, E2> const& rhs)
{
  return quat_subtraction<E1, E2>(lhs, rhs);
}

template <typename E, typename S,
  typename = typename ::std::enable_if<::std::is_arithmetic<S>::value>::type>
constexpr inline quat_scalar_multiplication<S, E>
operator*(S const a, quat_expression<typename E::value_type, E> const& rhs)
{
  return quat_scalar_multiplication<S, E>(a, rhs);
}

template <typename E, typename S,
  typename = typename ::std::enable_if<::std::is_arithmetic<S>::value>::type>
constexpr inline quat_scalar_multiplication<S, E>
operator*(quat_expression<typename E::value_type, E> const& lhs, S const a)
{
  return quat_scalar_multiplication<S, E>(a, lhs);
}

template <typename E1, typename E2>
constexpr inline quat<prod_type<typename E1::value_type,
  typename E2::value_type> >
operator*(quat_expression<typename E1::value_type, E1> const& l,
  quat_expression<typename E2::value_type, E2> const& r)
{
  return {
    l(3) * r(0) + r(3) * l(0) + l(1) * r(2) - l(2) * r(2),
    l(3) * r(1) + r(3) * l(1) - l(0) * r(2) + l(2) * r(0),
    l(3) * r(2) + r(3) * l(2) + l(0) * r(1) - l(1) * r(0),
    l(3) * r(3) - l(0) * r(0) - l(1) * r(1) - l(2) * r(2)};
}

namespace quat_eq_impl
{
#if !defined(__GNUC__)
  template <::std::size_t i, ::std::size_t... Is, typename E1, typename E2>
  inline constexpr typename ::std::enable_if<
    bool(!sizeof...(Is)), bool
  >::type
  get(E1 const& lhs, E2 const& rhs)
  {
    return lhs(i) == rhs(i);
  }

  template <::std::size_t i, ::std::size_t... Is, typename E1, typename E2>
  inline constexpr typename ::std::enable_if<
    bool(sizeof...(Is)), bool
  >::type
  get(E1 const& lhs, E2 const& rhs)
  {
    return (lhs(i) == rhs(i)) && get<Is...>(lhs, rhs);
  }
#elif defined(__GNUC__)
  template <::std::size_t, ::std::size_t... Is, typename E1, typename E2>
  bool get(E1 const& lhs, E2 const& rhs)
  {
    using value_type =
      prod_type<typename E1::value_type, typename E2::value_type>;
    typedef value_type vec_type __attribute__ ((vector_size(
      detail::map_extent<E1::dimension>{} * sizeof(value_type))));

    vec_type const a{lhs(0), lhs(Is)...};
    vec_type const b{rhs(0), rhs(Is)...};

    vec_type const v(a == b);

    bool r(v[0]);

    ::std::initializer_list<int>{(r = r && v[Is], 0)...};

    return r;
  }
#endif

  template <typename E1, typename E2, ::std::size_t... Is>
  inline constexpr typename E1::value_type
  get(E1 const& lhs, E2 const& rhs, detail::indices<Is...> const)
  {
    return get<Is...>(lhs, rhs);
  }
}

template <typename E1, typename E2>
inline constexpr bool
operator==(quat_expression<typename E1::value_type, E1> const& lhs,
  quat_expression<typename E2::value_type, E2> const& rhs)
{
  return quat_eq_impl::get(lhs, rhs, detail::make_indices<E1::dimenstion>());
}

template <typename E1, typename E2>
inline constexpr bool
operator!=(quat_expression<typename E1::value_type, E1> const& lhs,
  quat_expression<typename E2::value_type, E2> const& rhs)
{
  return !operator==(lhs, rhs);
}

template <typename E>
::std::ostream& operator<<(::std::ostream& os,
  quat_expression<typename E::value_type, E> const& u)
{
  os << '(';

  for (typename E::size_type i(0); i != E::dimension - 1; ++i)
  {
    os << u(i) << ", ";
  }

  os << u(E::dimension - 1) << ')';

  return os;
}

}

#endif // ETL_QUATERNION_HPP
